(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:UimodsCtrl
     * @description
     * # UimodsCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('UimodsCtrl', UimodsCtrl);

    UimodsCtrl.$inject = ['$scope', 'dataService', 'helper', '$location','$rootScope','cacheService'];

    function UimodsCtrl($scope, dataService, helper, $location,$rootScope,cacheService) {

        $scope.listAction = function() {
            $scope.xtotal = 0;
            $scope.pageSize = 10;
            $scope.getList();
        }

        //获取仓库列表
        $scope.getList = function(pageNo) {
            var query = {};
            var key = $rootScope.store_id + "UimodsCtrl";
            var cache = cacheService.get(key);
            if (pageNo || !cache) {
                $scope.pageNo = pageNo ? pageNo : 1;
                query = {
                    page_no: $scope.pageNo,
                    page_size: $scope.pageSize,
                };
                var subquery = {};

                if ($scope.uimods_id) {
                    subquery.uimods_id = $scope.uimods_id;
                }

                if ($scope.ctype) {
                    subquery.ctype = $scope.ctype;
                }

                if ($scope.types) {
                    subquery.types = $scope.types;
                }

                if ($scope.xmod && $scope.xmod.id) {
                    subquery.subtype = $scope.xmod.id
                }
                query.query = subquery;
                console.error("query",query);
                cacheService.set(key, query);
            } else {
                query = clone(cache);
                $scope.queryRcache(query);
            }

            dataService.uimodsGet(query).then(function(list) {
                if (list && list.uimods) {
                    $scope.uimods = list.uimods;
                    $scope.xtotal = list.total_count;
                }
            });
        }

        $scope.queryRcache = function(query) {
            console.error(" query", query);
            $scope.pageNo = query.page_no;
            console.error(" $scope.pageNo", $scope.pageNo);
            $scope.page_size = query.page_size;
            
            if (query.query.uimods_id) {
                $scope.uimods_id = query.query.uimods_id;
            }
            if (query.query.ctype) {
                $scope.ctype = query.query.ctype;
            }
            if (query.query.types) {
                $scope.types = query.query.types;
            }

            if (query.query.subtype) {
                $scope.xmod ={id:query.query.subtype}
            }
        }

        $scope.addAction = function() {
            var uimods_id = $location.search()["uimods_id"];
            if (uimods_id) {
                dataService.uimodsOne(uimods_id).then(function(result) {
                    if (result.uimods_id) {
                        $scope.uimods = result;
                    } else {
                        helper.myAlert("错误：" + result.Msg);
                    }
                })
            } else {
                $scope.uimods = {
                    store_id: "-1",
                    app_id: "-1",
                    ctype: "",
                    types: "",
                    subtype: "",
                    name: "",
                    data: {},
                    note: "",
                };
            }
            $scope.$watch('xmod', function(newvalue, oldvalue, scope) {
                if (newvalue) {
                    $scope.uimods.name = $scope.uimods.name ? $scope.uimods.name : newvalue.id + random(11111, 99999);
                    $scope.data = JSON.stringify(clone(newvalue.data));
                }
            }, true);
        }

        $scope.save = function() {
            var method = $scope.uimods.uimods_id ? "uimodsUpdate" : "uimodsAdd";
            var saveData = clone($scope.uimods);
            saveData.subtype = $scope.xmod.id;
            saveData.data = JSON.parse(clone($scope.data));

            dataService[method](saveData).then(function(result) {
                if (result.Msg) {
                    helper.myAlert("错误：" + result.Msg);
                } else {
                    gourl("#/uimods/list");
                }
            });
        }

        $scope.delete = function(uimods_id) {
            dataService.isvUimodsDelete(uimods_id).then(function(result) {
                if (result.Msg) {
                    helper.myAlert("错误：" + result.Msg);
                } else {
                    $scope.getList();
                }
            });
        }
    }
})();